<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item
        label="名称"
        prop="supplier"
        :class="{ 'is-required': !dataForm.id }"
      >
        <el-input v-model="dataForm.supplier" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item
        label="联系人"
        prop="contacts"
        :class="{ 'is-required': !dataForm.id }"
      >
        <el-input v-model="dataForm.contacts" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item
        label="手机号码"
        prop="telephone"
        :class="{ 'is-required': !dataForm.id }"
      >
        <el-input v-model="dataForm.telephone" placeholder="手机号码"></el-input>
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="电子邮箱"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="phoneNum">
        <el-input v-model="dataForm.phoneNum" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item label="传真" prop="fax">
        <el-input
          v-model="dataForm.fax"
          placeholder="传真"
        ></el-input>
      </el-form-item>
      <el-form-item label="预付款" prop="advanceIn">
        <el-input v-model="dataForm.advanceIn" placeholder="预付款"></el-input>
      </el-form-item>
      <el-form-item label="初期应收" prop="beginNeedGet">
        <el-input v-model="dataForm.beginNeedGet" placeholder="初期应收"></el-input>
      </el-form-item>
      <el-form-item label="初期应付" prop="beginNeedPay">
        <el-input
          v-model="dataForm.beginNeedPay"
          placeholder="初期应付"
        ></el-input>
      </el-form-item>
      <el-form-item label="末期应收" prop="allNeedGet">
        <el-input
          v-model="dataForm.allNeedGet"
          placeholder="末期应收"
        ></el-input>
      </el-form-item>
      <el-form-item label="末期应付" prop="allNeedPay">
        <el-input
          v-model="dataForm.allNeedPay"
          placeholder="末期应付"
        ></el-input>
      </el-form-item>
      <el-form-item label="税率" prop="taxRate">
        <el-input
          v-model="dataForm.taxRate"
          placeholder="税率"
        ></el-input>
      </el-form-item>
      <el-form-item label="开户行" prop="bank">
        <el-input
          v-model="dataForm.bank"
          placeholder="开户行"
        ></el-input>
      </el-form-item>
      <el-form-item label="账号" prop="account">
        <el-input
          v-model="dataForm.account"
          placeholder="账号"
        ></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="addres">
        <el-input
          v-model="dataForm.addres"
          placeholder="地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="dataForm.remark"
          placeholder="备注"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isEmail, isMobile } from "@/utils/validate";
export default {
  data() {
    return {
      parentValue: [],
      value1: "",
      value2: "",
      options: [
        {
          value: "1",
          label: "未营业",
        },
        {
          value: "2",
          label: "正常营业",
        },
        {
          value: "3",
          label: "暂停营业",
        },
        {
          value: "4",
          label: "终止营业",
        },
        {
          value: "5",
          label: "已除名",
        },
      ],
      value: "",
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        DepotName: "",
        contacts: "",
        telephone: "",
        email: "",
        fax: "",
        phoneNum: "",
        advanceIn: "",
        beginNeedGet: "",
        beginNeedPay: "",
        allNeedGet: "",
        allNeedPay: "",
        taxRate: "",
      },
      dataRule: {
        supplier: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        contacts: [{ required: true, message: "联系人不能为空", trigger: "blur" }],
        telephone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
        ],
        email: [{ required: true, message: "电子邮箱不能为空", trigger: "blur" }],
        phoneNum: [
          { required: true, message: "联系电话不能为空", trigger: "blur" },
        ],
        fax: [
          { required: true, message: "传真不能为空", trigger: "blur" },
        ],
        // advanceIn: [
        //   { required: true, message: "预付款不能为空", trigger: "blur" },
        // ],
        // beginNeedGet: [
        //   { required: true, message: "初期应收不能为空", trigger: "blur" },
        // ],
        // beginNeedPay: [
        //   { required: true, message: "初期应付不能为空", trigger: "blur" },
        // ],
        // allNeedGet: [
        //   { required: true, message: "末期应收不能为空", trigger: "blur" },
        // ],
        // allNeedPay: [
        //   { required: true, message: "末期应付不能为空", trigger: "blur" },
        // ],
        // commodityDecimal: [
        //   { required: true, message: "销售价不能为空", trigger: "blur" },
        // ],
        // taxRate: [
        //   { required: true, message: "税率不能为空", trigger: "blur" },
        // ],
      },
    };
  },
  methods: {
    // init(id) {
    //   // console.log(id);
    //   if(id){
    //     this.$http({
    //     url: this.$http.goods('/materialsExtend/getDetailList'),
    //     method: "get",
    //     params:({
    //         'materialId':id,
    //       })
    //   })
    //     .then((data) => {
    //       console.log(data.data.data.rows[0]);
    //       if (data.data.code === 200) {
    //         this.dataForm = data.data.data.rows[0];
    //         this.dataForm.id = id;
    //       }
    //       this.roleList = data && data.code === 0 ? data.list : [];
    //     })
    //     .then(() => {
    //       this.visible = true;
    //     });
    //   }
    // },
    init(id) {
      // console.log(id);
      this.dataForm.id = id || 0;
      this.$http({
        url: this.$http.adornUrl("/sys/role/select"),
        method: "get",
        params: this.$http.adornParams(),
      })
        .then(({ data }) => {
          this.roleList = data && data.code === 0 ? data.list : [];
        })
        .then(() => {
          this.visible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
          });
        })
        .then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.goods('/materialsExtend/getDetailList'),
              method: "get",
              params: {
                materialId: id,
              },
            }).then(({ data }) => {
              console.log(data);
              this.dataForm = data.data.rows[0];
              this.dataForm.id=id;
            });
          }
        });
    },

    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/organization/${
                !this.dataForm.id ? "addOrganization" : "update"
              }`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              supplier: this.dataForm.supplier,
              contacts: this.dataForm.contacts,
              telephone: this.dataForm.telephone,
              standard: this.dataForm.standard,
              phoneNum: this.dataForm.phoneNum,
              email: this.dataForm.email,
              sort: this.dataForm.sort,
              fax: this.dataForm.fax,
              advanceIn: this.dataForm.advanceIn,
              beginNeedPay: this.dataForm.beginNeedPay,
              allNeedGet: this.dataForm.allNeedGet,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
